<?php

require_once('../model/m_shifts.php');

class shifts{

	public $shiftdata = array();
	public $shiftchangesdata = array();
	public $weekdaydata = array();
	public $userdata = array();
	public $currentUser;
	
	
	public function actionView(){
		include_once("../views/v_session.php"); 
	}		
	
	public function actionViewShiftChange(){
		include_once("../views/v_session.php"); 
		include_once("../views/v_header_principal_shifts_ajax.html");
		include_once("../views/v_shifts_change.html");
		include_once("../views/v_footer_principal.html");
	}	
		
	public function actionViewChangeForm($username){
		$this->currentUser= $username;
		$this->weekdaydata = loadWeekdays();
		$this->shiftdata = loadShifts();		
		$this->userdata = loadUsers();		
		include_once("../views/_v_shifts_change_form.html");
	}	
			
	public function actionViewManageChanges($username){
		
		if($username == 'list'){
			include_once("../views/v_session.php");
			include_once("../views/v_header_principal_shifts_ajax.html");
			include_once("../views/v_shifts_change_management.html");
			include_once("../views/v_footer_principal.html");
			
		} else {			
			$this->currentUser= $username;
			$this->shiftchangesdata = loadShiftChanges($username);
			include_once("../views/_v_shifts_change_management.html");
		} 
	}	
	
	
	public function actionViewMyShift($username){
		
		
		// only collect data if a username is specified
		if ($username != 'yes'){
			$this->currentUser= $username;
			$this->shiftData = getShiftsUser($username); //permanent week shifts
			$this->shiftWeekData = getShiftsUserWeek($username); //this week's shifts
			include_once("../views/_v_shifts_myshift.html");
			
		} else {
			
			include_once("../views/v_session.php"); 
			include_once("../views/v_header_principal_shifts_ajax.html");
			include_once("../views/v_shifts_myshift.html");
			include_once("../views/v_footer_principal.html");
		}
	}		
	
	
	public function actionViewDay($day){
		if($day=='Today'){		
			$date = new DateTime("now");
			$date->setTimeZone(new DateTimeZone("America/Costa_Rica"));
			$day= $date->format('l');
		}	
		$this->currentDay = $day;
		$this->qShiftData = getQShifts($day); //Get shifts for today
		include_once("../views/_v_q_shifts_day.html");
	}
	
	public function actionCancelRequest($id,$username){

		if(cancelRequest($id,$username)){
			$this->actionViewManageChanges($username);
			exit;				
		} else {		
			echo "<h1> Error Cancelling Request</h1>";
			exit;
		}	
	}

	public function actionSubmitRequest($username,$date,$isShift,$isUser,$data){
	
		if(isValidDate($date)) {
			$result= false;
			
			$now = new DateTime('NOW');
			$now->setTimeZone(new DateTimeZone("America/Costa_Rica"));
			$today = $now->format('Y-m-d');
			
			if($date <= $today){
				echo "<h2>
					  <font color='red'>
					  Error: <i></font>You can only request a change for a date later than today. <br> </i>
					  <font color='blue'>
					  Please try again with a later date.
					  
					  </h2>";
				$this->actionViewChangeForm($username);		
				exit;
			}			
			
			if ($isShift == "true") {
				$result = submitRequestByShift($username,$date,$data);
			} elseif ($isUser == "true") {
				$result = submitRequestByUser($username,$date,$data);
			} else {
				echo "uncatched error at actionSubmitRequest in c_shifts";
				exit;
			}
			
			if($result) {
				echo "<span class='top-bar'><h1>Request Saved Successfully!!! &nbsp";
				echo "<image src='../images/check-icon.gif' width=20px height=20px></h1></span><br><br>";
				echo '<br><input type="button" value="Done" onclick="location.reload();"/>';				 
				exit;
			} else 
				echo "<h2> Error saving request</h2>";
			exit;
		} else {
			echo "<h2> The selected date is invalid, please try again</h2>";
			$this->actionViewChangeForm($username);
		}
	}		

}

$instancia= new shifts;

if(isset($_GET['viewDay'])){
	$instancia->actionViewDay($_GET['viewDay']);
} elseif(isset($_GET['viewMyShift'])){
	$instancia->actionViewMyShift($_GET['viewMyShift']);
} elseif(isset($_GET['viewShiftChange'])){
	$instancia->actionViewShiftChange();
} elseif(isset($_GET['viewChangeForm'])){
	$instancia->actionViewChangeForm($_GET['viewChangeForm']);
} elseif(isset($_GET['manageChanges'])){
	$instancia->actionViewManageChanges($_GET['manageChanges']);
} elseif(isset($_GET['cancel'])){
	$instancia->actionCancelRequest($_GET['cancel'],$_GET['username']);
}elseif(isset($_GET['requestSubmit'])){
	$instancia->actionSubmitRequest($_GET['requestSubmit'],$_GET['date'],$_GET['byShift'],$_GET['byUser'],$_GET['data']);
} else {
	$instancia->actionView();
}

?>